<template>
  <div>
    <!-- 1.定义面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>员工资料</el-breadcrumb-item>
      <el-breadcrumb-item>用户资料</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 2.定义卡片视图 -->
    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="9">
          <!-- 3.定义搜索框 -->
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getUserList">
            <el-button slot="append" icon="el-icon-search" @click="getUserList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <!-- 定义添加按钮-->
          <el-button type="primary" @click="dialogVisible =true">添加用户</el-button>
        </el-col>
      </el-row>
      <el-table :data="itemList" style="width: 100%;" border stripe>
        <el-table-column type="index" label="工号" width="50px"></el-table-column>
        <el-table-column prop="username" label="姓名"></el-table-column>
        <el-table-column prop="sex" label="性别"></el-table-column>
        <el-table-column prop="birthday" label="出生日期" width="100%"></el-table-column>
        <el-table-column prop="number" label="身份证号" width="200px"></el-table-column>
        <el-table-column prop="marital" label="婚姻状况"></el-table-column>
        <el-table-column prop="natio" label="民族"></el-table-column>
        <el-table-column prop="origo" label="籍贯" width="150px"></el-table-column>
        <el-table-column prop="email" label="电子邮箱" width="200px"></el-table-column>
        <el-table-column prop="iphone" label="手机号码" width="200px"></el-table-column>
        <el-table-column prop="address" label="联系地址" width="200px"></el-table-column>
        <el-table-column prop="department" label="所属部门" width="100px"></el-table-column>
        <el-table-column prop="position" label="职位" width="100px"></el-table-column>
        <el-table-column prop="rank" label="职称"></el-table-column>
        <el-table-column prop="hire" label="聘用形式"></el-table-column>
        <el-table-column prop="ondata" label="入职时间" width="100px"></el-table-column>
        <el-table-column prop="education" label="最高学历"></el-table-column>

        <el-table-column prop="status" label="状态" width="100px">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.status" @change="updateStatus(scope.row)" active-color="#13ce66"
            inactive-color="#ff4949">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200px">
          <template slot-scope="scope">
            <el-button type="primary" size="small" @click="updateUserBtn(scope.row)">编辑</el-button>
            <el-button type="danger" size="small" @click="deleteUser(scope.row)">删除</el-button>
          </template>

        </el-table-column>
      </el-table>
      <!-- 定义分页组件 关于分页插件的属性说明
            1.@size-change 如果分页中每页实现的条数发生变化 则会触发这个该事件
            2.@current-change 当页数发生改变时,触发该函数
            3.current-page 当前选中的页
            4.page-sizes   页面数据展现的选项
            5.page-size    默认选中的页数
            6.layout    分页中展现的数据种类
            7.total     记录总数 -->

      <el-pagination
      @size-change="handleSizeChange"
       @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[5, 10, 20, 40]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
      <!-- 编辑用户新增对话框 visible.sync 控制对话框的显示-->
      <el-dialog title="添加用户" :visible.sync="dialogVisible" width="50%" @close="closeDialog" >

        <!-- 定义用户提交的表单数据 -->
        <el-form :model="addUserModel" :rules="rules" ref="addUserRef" label-width="100px" class="demo-ruleForm">


          <el-row>
            <el-col :span='8'>
              <el-form-item label="姓名" prop="username">
                <el-input v-model="addUserModel.username" style="width: 140px;"></el-input>
              </el-form-item>
            </el-col>
            <!--     <el-col span="8">
             </el-col> -->
            <el-col :span="8">
              <el-form-item label="籍贯" prop="origo" >
                <el-input v-model="addUserModel.origo" style="width: 140px;"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="职称" prop="rank" >
                <select v-model="addUserModel.rank">
                  <option label="请选择"></option>
                  <option label="初级" value="1"></option>
                  <option label="中级" value="2"></option>
                  <option label="高级" value="3"></option>

                </select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span='8'>

              <el-form-item label="性别" prop="sex" >
                <el-input v-model="addUserModel.sex" style="width: 140px;"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span='8'>
              <el-form-item label="电子邮箱" prop="email" >
                <el-input v-model="addUserModel.email" style="width: 140px;"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span='8'>
              <el-form-item label="聘用形式" prop="hire" >
                <select v-model="addUserModel.hire">
                  <option label="请选择"></option>
                  <option label="工资直聘" value="1"></option>
                  <option label="劳务派遣" value="2"></option>


                </select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span='8'>
              <el-form-item label="出生日期" prop="birthday" >
                <input type="date" v-model="addUserModel.birthday" style="140px" />
              </el-form-item>
            </el-col>

            <el-col :span='8'>
              <el-form-item label="手机号" prop="iphone" >
                <el-input v-model="addUserModel.iphone" style="width: 140px;"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span='8'>
              <el-form-item label="入职时间" prop="ondata" >
                <input type="date" v-model="addUserModel.ondata" style="140px" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span='8'>
              <el-form-item label="身份证" prop="number" >
                <el-input v-model="addUserModel.number" style="width: 140px;"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span='8'>
              <el-form-item label="联系地址" prop="address" >
                <el-input v-model="addUserModel.address" style="width: 140px;"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span='8'>
              <el-form-item label="最高学历" prop="education" >
                <select v-model="addUserModel.education">
                  <option label="请选择"></option>
                  <option label="初中" value="1"></option>
                  <option label="中专" value="2"></option>
                  <option label="高中" value="3"></option>
                  <option label="大专" value="4"></option>
                  <option label="本科" value="5"></option>
                  <option label="研究生" value="6"></option>
                </select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span='8'>
              <el-form-item label="婚姻状况" prop="marital">
                <select v-model="addUserModel.marital">
                  <option label="请选择"></option>
                  <option label="未婚" value="1"></option>
                  <option label="已婚" value="2"></option>
                </select>
              </el-form-item>
            </el-col>
            <el-col :span='8'>
              <el-form-item label="所属部门" prop="department" >
                <select v-model="addUserModel.department">
                  <option label="请选择"></option>
                  <option label="JAVA开发部" value="1"></option>
                  <option label="web开发部" value="2"></option>
                </select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span='8'>
              <el-form-item label="民族" prop="natio" >
                <el-input v-model="addUserModel.natio" style="width: 140px;"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span='8'>
              <el-form-item label="职位" prop="position">
                <select v-model="addUserModel.position">
                  <option label="请选择"></option>
                  <option label="项目经理" value="1"></option>
                  <option label="总监" value="2"></option>
                  <option label="程序员" value="3"></option>

                </select>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <span slot="footer" class="dialog-footer" span="8">
          <el-button @click="dialogVisible = false" >取 消</el-button>
          <el-button type="primary" @click="addUserBtn">确 定</el-button>
        </span>
      </el-dialog>

       <el-dialog title="修改用户" :visible.sync="updateDialogVisible" width="50%" @close="closeUpdateDialog" >


         <el-form :model="updateUserModel" :rules="rules" ref="updateUserRef" class="demo-ruleForm">


           <el-row>
             <el-col :span='8'>
               <el-form-item label="姓名" prop="username">
                 <el-input v-model="updateUserModel.username" style="width: 140px;"></el-input>
               </el-form-item>
             </el-col>

             <el-col :span="8">
               <el-form-item label="籍贯" prop="origo" >
                 <el-input v-model="updateUserModel.origo" style="width: 140px;"></el-input>
               </el-form-item>
             </el-col>
             <el-col :span="8">
               <el-form-item label="职称" prop="rank" >
                 <select v-model="updateUserModel.rank">
                   <option label="请选择"></option>
                   <option label="初级" value="1"></option>
                   <option label="中级" value="2"></option>
                   <option label="高级" value="3"></option>

                 </select>
               </el-form-item>
             </el-col>
           </el-row>
           <el-row>
             <el-col :span='8'>

               <el-form-item label="性别" prop="sex" >
                 <el-input v-model="updateUserModel.sex" style="width: 140px;"></el-input>
               </el-form-item>
             </el-col>
             <el-col :span='8'>
               <el-form-item label="电子邮箱" prop="email" >
                 <el-input v-model="updateUserModel.email" style="width: 140px;"></el-input>
               </el-form-item>
             </el-col>
             <el-col :span='8'>
               <el-form-item label="聘用形式" prop="hire" >
                 <select v-model="updateUserModel.hire">
                   <option label="请选择"></option>
                   <option label="工资直聘" value="1"></option>
                   <option label="劳务派遣" value="2"></option>


                 </select>
               </el-form-item>
             </el-col>
           </el-row>
           <el-row>
             <el-col :span='8'>
               <el-form-item label="出生日期" prop="birthday" >
                 <input type="date" v-model="updateUserModel.birthday" style="140px" />
               </el-form-item>
             </el-col>

             <el-col :span='8'>
               <el-form-item label="手机号" prop="iphone" >
                 <el-input v-model="updateUserModel.iphone" style="width: 140px;"></el-input>
               </el-form-item>
             </el-col>
             <el-col :span='8'>
               <el-form-item label="入职时间" prop="ondata" >
                 <input type="date" v-model="updateUserModel.ondata" style="140px" />
               </el-form-item>
             </el-col>
           </el-row>
           <el-row>
             <el-col :span='8'>
               <el-form-item label="身份证" prop="number" >
                 <el-input v-model="updateUserModel.number" style="width: 140px;"></el-input>
               </el-form-item>
             </el-col>
             <el-col :span='8'>
               <el-form-item label="联系地址" prop="address" >
                 <el-input v-model="updateUserModel.address" style="width: 140px;"></el-input>
               </el-form-item>
             </el-col>
             <el-col :span='8'>
               <el-form-item label="最高学历" prop="education" >
                 <select v-model="updateUserModel.education">
                   <option label="请选择"></option>
                   <option label="初中" value="1"></option>
                   <option label="中专" value="2"></option>
                   <option label="高中" value="3"></option>
                   <option label="大专" value="4"></option>
                   <option label="本科" value="5"></option>
                   <option label="研究生" value="6"></option>
                 </select>
               </el-form-item>
             </el-col>
           </el-row>
           <el-row>
             <el-col :span='8'>
               <el-form-item label="婚姻状况" prop="marital">
                 <select v-model="updateUserModel.marital">
                   <option label="请选择"></option>
                   <option label="未婚" value="1"></option>
                   <option label="已婚" value="2"></option>
                 </select>
               </el-form-item>
             </el-col>
             <el-col :span='8'>
               <el-form-item label="所属部门" prop="department" >
                 <select v-model="updateUserModel.department">
                   <option label="请选择"></option>
                   <option label="JAVA开发部" value="1"></option>
                   <option label="web开发部" value="2"></option>
                 </select>
               </el-form-item>
             </el-col>
           </el-row>
           <el-row>
             <el-col :span='8'>
               <el-form-item label="民族" prop="natio" >
                 <el-input v-model="updateUserModel.natio" style="width: 140px;"></el-input>
               </el-form-item>
             </el-col>
             <el-col :span='8'>
               <el-form-item label="职位" prop="position" >
                 <select v-model="updateUserModel.position">
                   <option label="请选择"></option>
                   <option label="项目经理" value="1"></option>
                   <option label="总监" value="2"></option>
                   <option label="程序员" value="3"></option>

                 </select>
               </el-form-item>
             </el-col>
           </el-row>
         </el-form>

 <span slot="footer" class="dialog-footer">
        <el-button @click="updateDialogVisible = false" >取 消</el-button>
        <el-button type="primary" @click="updateUser">确 定</el-button>
      </span>

         </el-dialog>
    </el-card>


  </div>
</template>

<script>
  export default {
    data() {
      const checkEmail = (rule, value, callback) => {
        //定义邮箱的正则表达式  JS中用/来表示正则表达式的开始和结束
        const emailRege = /^[a-zA-Z0-9-_]+@[a-zA-Z0-9-_]+\.[a-zA-Z0-9-_]+$/
        //正则表达式语法校验  test(xx) 校验成功 返回true  校验失败返回false
        if (emailRege.test(value)) {
          //表示邮箱合法 正确返回
          return callback()
        }
        callback(new Error('请填写正确的邮箱地址'))
      }

      //校验手机号的邮箱规则
      const checkPhone = (rule, value, callback) => {

        //定义校验手机号的正则语法
        const phoneRege = /^1[34578][0-9]{9}$/
        if (phoneRege.test(value)) {

          return callback()
        }
        callback(new Error('请填写正确的手机号'))

      }
      return {
        queryInfo: {
          query: '',
          pageNum: 1,
          pageSize: 5,


        },


        itemList: [],
        total: 0,
        dialogVisible: false,
        addUserModel: {
          username: '',
          sex: '',
          birthday: '',
          number: '',
          marital: '',
          natio: '',
          origo: '',
          email: '',
          iphone: '',
          address: '',
          department: '',
          rank: '',

          position: '',
          hire: '',
          ondata: '',
          education: '',
        },
        rules: {
          username: [{
              required: true,
              message: '姓名',
              trigger: 'blur',
              },
             {
              min: 2,
              max: 5,
              message: '长度在 2 到 5 个字符',
              trigger: 'blur'
            }
          ],
          sex: [{
              required: true,
              message: '性别',
              trigger: 'blur'
            },
            {
              min: 1,
              max: 1,
              message: '长度在1个字符',
              trigger: 'blur'
            }
          ],
          birthday: [{
            required: true,
            message: '出生  日期',
            trigger: 'blur'
          }],
          number: [{
              required: true,
              message: '身份证',
              trigger: 'blur'
            },
            {
              min: 18,
              max: 18,
              message: '长度在18个字符',
              trigger: 'blur'
            }
          ],
          marital: [{
            required: true,
            message: '婚姻状况',
            trigger: 'change'
          }],
          natio: [{
              required: true,
              message: '民族',
              trigger: 'blur'
            },
            {
              min: 1,
              max: 10,
              message: '长度在2-10个字符',
              trigger: 'blur'
            }
          ],
          origo: [{
              required: true,
              message: '籍贯',
              trigger: 'blur'
            },
            {
              min: 2,
              max: 20,
              message: '长度在2-20个字符',
              trigger: 'blur'
            }
          ],
          email: [{
              required: true,
              message: '电子邮箱',
              trigger: 'blur'
            },
            {
              min: 2,
              max: 20,
              message: '长度在2-20个字符',
              trigger: 'blur'
            },
            {
              validator: checkEmail,
              trigger: 'blur'
            }
          ],
         // iphone: [{
         //     required: true,
         //     message: '手机号',
         //     trigger: 'blur'
         //   },
         //   {
         //     min: 1,
         //     max: 11,
         //     message: '长度在11个数字',
         //     trigger: 'blur'
         //   },

         // ],
         iphone: [{
             required: true,
             message: '请输入手机号',
             trigger: 'blur'
           },
           {
             min: 3,
             max: 30,
             message: '长度在 3 到 30 个字符',
             trigger: 'blur'
           },
           {
             validator: checkPhone,
             trigger: 'blur'
           }
         ],
          address: [{
              required: true,
              message: '联系地址',
              trigger: 'blur'
            },
            {
              min: 10,
              max: 30,
              message: '长度在10-30个数字',
              trigger: 'blur'
            }
          ],
          department: [{
            required: true,
            message: '所属部门',
            trigger: 'change'
          }],
          rank: [{
            required: true,
            message: '职称',
            trigger: 'change'
          }],
          position: [{
            required: true,
            message: '职位',
            trigger: 'change'
          }],
          hire: [{
            required: true,
            message: '聘用形式',
            trigger: 'change'
          }],
          ondata: [{
            required: true,
            message: '入职日期',
            trigger: 'blur',
          }],
          education: [{
            required: true,
            message: '最高学历',
            trigger: 'change'
          }],
        },
        updateDialogVisible: false,
        updateUserModel: {

        }
      }
    },
    methods: {
      async getUserList() {
        const {
          data: result
        } = await this.$http.get('/item/getItemList',{
           params: this.queryInfo
        })

        if (result.status !== 200) return this.$message.error("用户列表查询失败")
        this.itemList = result.data.rows
        this.total=result.data.total
        console.log(this.itemList)
        console.log(this.total)
      },
      async updateStatus(item){
         //实现用户状态修改  注意使用模版字符串  ES6中提出的新用法 ${key}
          const {
            data: result
            } = await this.$http.put(`/item/status/${item.id}/${item.status}`)

        if(result.status !== 200) return this.$message.error("用户状态修改失败!")
        this.$message.success("用户状态修改成功!")
      },
      async deleteUser(item) {
        //1.消息确认框
        const result = await this.$confirm('此操作将永久删除 ' + item.username + ', 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(error => error)

        //如果确认  confirm  如果取消 cancel
        if (result !== 'confirm') {
          return this.$message.info("删除取消")
        }
        const {
          data: result2
        } = await this.$http.delete(`/item/li/${item.id}`)
        if (result2.status !== 200) return this.$message.error("删除失败")
        this.$message.success("删除成功")
        //重新加载 数据
        this.getUserList()
      },
      async updateUserBtn(item) {
        this.updateDialogVisible = true

        const {
          data: result

        // } = await this.$http.get("/aa/item")
        } = await this.$http.get(`/item/aa/${item.id}` )
          console.log("我在查询")
        if (result.status !== 200) return this.$message.error("用户查询失败")
        this.updateUserModel = result.data
      },
        closeUpdateDialog(){
        //重置表格数据
        this.$refs.updateUserRef.resetFields()
      },

      //校验用户数据
      addUserBtn() {
        this.$refs.addUserRef.validate(async valid => {
          //如果校验失败 则停止数据
          if (!valid) return
          //console.log(this.addUserModel)
          const {
            data: result
          } = await this.$http.post('/item/addUser', this.addUserModel)
          if (result.status !== 200) return this.$message.error("用户新增失败")
          this.$message.success("用户新增成功")
          //关闭对话框
          this.dialogVisible = false
          //重新获取用户列表
          this.getUserList()

        })
      },
      handleSizeChange(pageSize) {
        //console.log("每页展现的条数"+pageSize)
        this.queryInfo.pageSize = pageSize
        this.getUserList()
      },
      handleCurrentChange(pageNum) {
        //console.log("页数:"+pageNum)
        this.queryInfo.pageNum = pageNum
        this.getUserList()
      },
      closeDialog() {
        //重置表格数据
        this.$refs.addUserRef.resetFields()
      },
      updateUser(){
        //1.预校验数据
        this.$refs.updateUserRef.validate(
        async valid => {
            
         
           if(!valid)  return this.$message.error("表单验证没有通过")
           //根据接口文档要求封装数据
           let item = {}
           item.username=this.updateUserModel.username
           item.sex=this.updateUserModel.sex
           item.birthday=this.updateUserModel.birthday
           item.number=this.updateUserModel.number
           item.marital=this.updateUserModel.margin
           item.natio=this.updateUserModel.natio
           item.origo=this.updateUserModel.origo
           item.email=this.updateUserModel.email
           item.iphone=this.updateUserModel.iphone
           item.address=this.updateUserModel.address
           item.department=this.updateUserModel.department
           item.position=this.updateUserModel.position
           item.rank=this.updateUserModel.rank
           item.hire=this.updateUserModel.hire
           item.education=this.updateUserModel.education
           console.log(item)
           const {data: result} = await this.$http.put(`/item/updateItem`,this.updateUserModel)
           if(result.status !== 200) return this.$message.error("用户修改失败")
           this.$message.success("用户更新成功")
           this.updateDialogVisible = false
           this.getUserList()
        })
      },

    },
    //利用钩子函数实现数据查询
    created() {
      this.getUserList()

    }
  }
</script>

<style lang="less" scoped>


</style>
